package cn.edu.xmu.oomall.customer.controller;


import cn.edu.xmu.javaee.core.aop.LoginUser;
import cn.edu.xmu.javaee.core.exception.BusinessException;
import cn.edu.xmu.javaee.core.model.ReturnNo;
import cn.edu.xmu.javaee.core.model.ReturnObject;
import cn.edu.xmu.javaee.core.model.dto.UserDto;
import cn.edu.xmu.oomall.customer.service.CustomerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static cn.edu.xmu.javaee.core.model.Constants.PLATFORM;

@RestController /*Restful的Controller对象*/
@RequestMapping(value = "/shops", produces = "application/json;charset=UTF-8")
@RequiredArgsConstructor
@Slf4j
public class ShopCustomerController {

    private final CustomerService customerService;

    /**
     * 平台管理员封禁买家
     * @param dId
     * @param id
     * @param user
     * @return
     */
    @PutMapping("/{did}/customers/{id}/ban")
    public ReturnObject updateCustomerBanStatus(@PathVariable(value = "did") Long dId,
                                                @PathVariable Long id,
                                                @LoginUser UserDto user) {
        if (!PLATFORM.equals(dId)) {
            throw new BusinessException(ReturnNo.RESOURCE_ID_OUTSCOPE, String.format(ReturnNo.RESOURCE_ID_OUTSCOPE.getMessage(), "顾客", id, dId));
        }
        customerService.updateCustomerBanStatus(id, user);
        return new ReturnObject();
    }

    /**
     * 平台管理员解禁买家
     * @param dId
     * @param id
     * @param user
     * @return
     */
    @PutMapping("/{did}/customers/{id}/release")
    public ReturnObject updateCustomerReleaseStatus(@PathVariable(value = "did") Long dId,
                                                    @PathVariable Long id,
                                                    @LoginUser UserDto user) {
        if (!PLATFORM.equals(dId)) {
            throw new BusinessException(ReturnNo.RESOURCE_ID_OUTSCOPE, String.format(ReturnNo.RESOURCE_ID_OUTSCOPE.getMessage(), "顾客", id, dId));
        }
        customerService.updateCustomerReleaseStatus(id, user);
        return new ReturnObject();
    }
}
